package com.yami.shop.platform.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.model.MeltCloudUltraGroupApply;
import com.yami.shop.bean.model.MeltCloudUltraGroupCost;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.MeltCloudUltraGroupApplyService;
import com.yami.shop.service.MeltCloudUltraGroupCostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/admin/ultra/group/cost")
@Tag(name = "融云超级群资费管理")
@AllArgsConstructor
public class MeltCloudUltraGroupCostController {
    
    private final MeltCloudUltraGroupCostService ultraGroupCostService;
    
    private final MeltCloudUltraGroupApplyService ultraGroupApplyService;
    
    @GetMapping("/page")
    @Operation(summary =  "分页获取超级群聊资费标准")
    public ServerResponseEntity<IPage<MeltCloudUltraGroupCost>> page(@ParameterObject MeltCloudUltraGroupCost cost, PageParam<MeltCloudUltraGroupCost> pageParam) {
        IPage<MeltCloudUltraGroupCost> page = ultraGroupCostService.page(pageParam, cost);
        return ServerResponseEntity.success(page);
    }


    @GetMapping("/one")
    @Operation(summary =  "分页获取超级群聊资费标准")
    public ServerResponseEntity<MeltCloudUltraGroupCost> getById(@RequestParam Long costId) {
        MeltCloudUltraGroupCost cost = ultraGroupCostService.getById(costId);
        return ServerResponseEntity.success(cost);
    }


    @PostMapping("/add")
    @Operation(summary =  "新增")
    public ServerResponseEntity add(@RequestBody MeltCloudUltraGroupCost cost) {
        LambdaQueryWrapper<MeltCloudUltraGroupCost> qw = Wrappers.lambdaQuery(MeltCloudUltraGroupCost.class)
                .eq(MeltCloudUltraGroupCost::getMonths, cost.getMonths()).eq(MeltCloudUltraGroupCost::getEnergyValue, cost.getEnergyValue()).eq(MeltCloudUltraGroupCost::getIsRenew, cost.getIsRenew());
        if (cost.getIsRenew() == 0) {
            cost.setRenewValue(null);
            qw.isNull(MeltCloudUltraGroupCost::getRenewValue);
        } else {
            qw.eq(MeltCloudUltraGroupCost::getRenewValue, cost.getRenewValue());
        }
        qw.eq(MeltCloudUltraGroupCost::getIsDel, 0);
        MeltCloudUltraGroupCost one = ultraGroupCostService.getOne(qw);
        if (Objects.nonNull(one)) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "已存在相同配置！");
        }
        ultraGroupCostService.save(cost);
        return ServerResponseEntity.success();
    }
    
    @PostMapping("/edit")
    @Operation(summary =  "修改")
    public ServerResponseEntity update(@RequestBody MeltCloudUltraGroupCost cost) {
        if (Objects.isNull(cost.getId())) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "ID不能为空！");
        }
        List<MeltCloudUltraGroupApply> list = ultraGroupApplyService.list(Wrappers.lambdaQuery(MeltCloudUltraGroupApply.class).eq(MeltCloudUltraGroupApply::getCostId, cost.getId()));
        if (CollUtil.isNotEmpty(list)) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "该资费已被引用不能修改！");
        }
        LambdaQueryWrapper<MeltCloudUltraGroupCost> wrapper = Wrappers.lambdaQuery(MeltCloudUltraGroupCost.class)
                .eq(MeltCloudUltraGroupCost::getMonths, cost.getMonths()).eq(MeltCloudUltraGroupCost::getEnergyValue, cost.getEnergyValue()).eq(MeltCloudUltraGroupCost::getIsRenew, cost.getIsRenew());
        if (cost.getIsRenew() == 0) {
            cost.setRenewValue(null);
            wrapper.isNull(MeltCloudUltraGroupCost::getRenewValue);
        } else {
            wrapper.eq(MeltCloudUltraGroupCost::getRenewValue, cost.getRenewValue());
        }
        wrapper.eq(MeltCloudUltraGroupCost::getIsDel, 0).ne(MeltCloudUltraGroupCost::getId, cost.getId());
        MeltCloudUltraGroupCost one = ultraGroupCostService.getOne(wrapper);
        if (Objects.nonNull(one)) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "已存在相同配置！");
        }
        ultraGroupCostService.updateById(cost);
        return ServerResponseEntity.success();
    }

    
    @DeleteMapping("/{id}")
    @Operation(summary =  "删除")
    public ServerResponseEntity delete(@PathVariable Long id) {
        List<MeltCloudUltraGroupApply> list = ultraGroupApplyService.list(Wrappers.lambdaQuery(MeltCloudUltraGroupApply.class).eq(MeltCloudUltraGroupApply::getCostId, id));
        if (CollUtil.isNotEmpty(list)) {
            throw new YamiShopBindException(ResponseEnum.SHOW_FAIL, "该资费已被引用不能删除！");
        }
        ultraGroupCostService.update(Wrappers.lambdaUpdate(MeltCloudUltraGroupCost.class).eq(MeltCloudUltraGroupCost::getId, id).set(MeltCloudUltraGroupCost::getIsDel, 1));
        return ServerResponseEntity.success();
    }


    @GetMapping("/{id}")
    @Operation(summary =  "隐藏")
    public ServerResponseEntity hide(@PathVariable Long id) {
        MeltCloudUltraGroupCost cost = ultraGroupCostService.getById(id);
        if (cost.getIsHide() == 0) {
            cost.setIsHide(1);
        } else {
            cost.setIsHide(0);
        }
        ultraGroupCostService.updateById(cost);
        return ServerResponseEntity.success();
    }




}
